#target illustrator
(function () {
    if (app.documents.length === 0) { alert("先打开文档"); return; }
    var doc = app.activeDocument;
    if (!doc.selection || doc.selection.length === 0) { alert("请至少选择 1 个编组与 1 条路径"); return; }

    // --- 选项：中心基于几何/可见边界（描边/效果） ---
    var USE_VISIBLE_BOUNDS = false;

    // --- 工具：兼容旧版的 trim ---
    function sTrim(s) { return (s || "").replace(/^\s+|\s+$/g, ""); }

    // --- 选择模式：1=左端点；2=右端点 ---
    var ans = prompt("输入 1 对齐左端点；输入 2 对齐右端点", "1", "选择对齐端点");
    if (ans === null) return;
    ans = sTrim(String(ans));
    if (ans !== "1" && ans !== "2") { alert("请输入 1 或 2"); return; }
    var alignRight = (ans === "2");

    // --- 收集：恰好 1 个 GroupItem（模板） + 多条路径（Path/CompoundPath） ---
    var group = null;
    var paths = []; // {type:"path"|"compound", ref:<item>}
    for (var i = 0; i < doc.selection.length; i++) {
        var it = doc.selection[i];
        var t = it.typename;
        if (t === "GroupItem") {
            if (group) { alert("请选择且仅选择 1 个 GroupItem 作为模板。"); return; }
            group = it;
        } else if (t === "PathItem") {
            paths.push({ type: "path", ref: it });
        } else if (t === "CompoundPathItem") {
            paths.push({ type: "compound", ref: it });
        }
    }
    if (!group) { alert("未找到 GroupItem，请选择 1 个编组作为模板。"); return; }
    if (paths.length === 0) { alert("未找到路径，请至少选择 1 条路径（Path 或 CompoundPath）。"); return; }

    // --- 计算 Group 中心 ---
    function groupCenter(g) {
        var b = USE_VISIBLE_BOUNDS ? g.visibleBounds : g.geometricBounds; // [L,T,R,B]
        return [ (b[0] + b[2]) / 2, (b[1] + b[3]) / 2 ];
    }

    // --- 取路径“左端点/右端点”（按 x 最小/最大） ---
    function endAnchor_PathItem(p, rightSide) {
        var pp = p.pathPoints; if (!pp || pp.length === 0) return null;
        var pickX = pp[0].anchor[0], pickY = pp[0].anchor[1];
        for (var i = 1; i < pp.length; i++) {
            var a = pp[i].anchor;
            if (rightSide) { if (a[0] > pickX) { pickX = a[0]; pickY = a[1]; } }
            else           { if (a[0] < pickX) { pickX = a[0]; pickY = a[1]; } }
        }
        return [pickX, pickY];
    }
    function endAnchor_Compound(cp, rightSide) {
        if (!cp || !cp.pathItems || cp.pathItems.length === 0) return null;
        var got = false, pickX = 0, pickY = 0;
        for (var i = 0; i < cp.pathItems.length; i++) {
            var p = cp.pathItems[i], pp = p.pathPoints;
            for (var j = 0; j < pp.length; j++) {
                var a = pp[j].anchor;
                if (!got) { pickX = a[0]; pickY = a[1]; got = true; }
                else {
                    if (rightSide) { if (a[0] > pickX) { pickX = a[0]; pickY = a[1]; } }
                    else           { if (a[0] < pickX) { pickX = a[0]; pickY = a[1]; } }
                }
            }
        }
        return got ? [pickX, pickY] : null;
    }

    // --- 放置 ---
    var placed = 0;
    for (var k = 0; k < paths.length; k++) {
        var target = null;
        if (paths[k].type === "path")          target = endAnchor_PathItem(paths[k].ref, alignRight);
        else /* compound */                    target = endAnchor_Compound(paths[k].ref, alignRight);
        if (!target) continue;

        try {
            var dup = group.duplicate();           // 复制模板
            var c = groupCenter(dup);              // 复制品中心
            dup.translate(target[0] - c[0], target[1] - c[1]); // 平移对齐
            placed++;
        } catch (e) {
            alert("复制/移动失败：\n" + e);
        }
    }

    if (placed === 0) alert("未能放置任何副本，请检查所选路径是否包含锚点。");
})();
